package com.system.controller;

import com.system.pojo.SysUser;
import com.system.service.AmountsService;
import com.system.service.ContractService;
import com.system.vo.Amounts;
import com.system.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author yjymm
 * @date 2019-12-15 21:31
 */
@Controller
@ResponseBody
@RequestMapping("/amounts")
public class AmountsController {

    @Autowired
    private AmountsService amountsService;

    @Autowired
    private ContractService contractService;

    @RequestMapping("/client")
    public ResultVo<Amounts> selectClientAmounts(){
        ResultVo<Amounts> resultVo = null;
        try {
            resultVo = amountsService.selectClientAmounts();
        }catch (Exception e){
            e.printStackTrace();
            resultVo.setMessage("查询失败");
        }finally {
            return resultVo;
        }
    }

    @RequestMapping("/product")
    public ResultVo<Amounts> selectProductAmounts(){
        ResultVo<Amounts> resultVo = null;
        try {
            resultVo = amountsService.selectProductAmounts();
        }catch (Exception e){
            e.printStackTrace();
            resultVo.setMessage("查询失败");
        }finally {
            return resultVo;
        }
    }

    @RequestMapping("/company/all")
    public ResultVo<Amounts> companyAllAmounts(){
        ResultVo<Amounts> resultVo = null;
        try {
            resultVo = amountsService.companyAllAmounts();
        }catch (Exception e){
            e.printStackTrace();
            resultVo.setMessage("查询失败");
        }finally {
            return resultVo;
        }
    }


    @RequestMapping("/company/time")
    public ResultVo<Amounts> companyAllAmountsTime(
            @RequestParam @DateTimeFormat(pattern = "MM/dd/yyyy")Date time1,
            @RequestParam @DateTimeFormat(pattern = "MM/dd/yyyy")Date time2
            ){
        ResultVo<Amounts> resultVo = null;
        try {
            resultVo = amountsService.companyAllAmountsTime(time1,time2);
        }catch (Exception e){
            e.printStackTrace();
            resultVo.setMessage("查询失败");
        }finally {
            return resultVo;
        }
    }


    @RequestMapping("/sysUser/all")
    public ResultVo<Amounts> sysUserAllAmounts(HttpSession session){
        ResultVo<Amounts> resultVo = null;
        SysUser sysUser =(SysUser) session.getAttribute("sysUser");
        try {
            if (sysUser == null){
                resultVo.setMessage("查询个人销售额失败");
            }else{
                Integer uid = sysUser.getId();
                resultVo = amountsService.sysUserAllAmounts(uid);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultVo.setMessage("查询个人销售额失败");
        }finally {
            return resultVo;
        }
    }


    @RequestMapping("/sysUser/time")
    public ResultVo<Amounts> sysUserAmountsTime(
            @RequestParam @DateTimeFormat(pattern = "MM/dd/yyyy")Date time1,
            @RequestParam @DateTimeFormat(pattern = "MM/dd/yyyy")Date time2,
            HttpSession session){
        ResultVo<Amounts> resultVo = null;
        SysUser sysUser =(SysUser) session.getAttribute("sysUser");
        try {
            if (sysUser == null){
                resultVo.setMessage("查询个人销售额失败");
            }else{
                Integer uid = sysUser.getId();
                resultVo = amountsService.sysUserAmountsTime(uid,time1,time2);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultVo.setMessage("查询个人销售额失败");
        }finally {
            return resultVo;
        }
    }
}
